package cn.test;

import cn.lau.ApiApplication;
import cn.lau.auth.entity.SysRole;
import cn.lau.auth.entity.SysUser;
import cn.lau.auth.repository.SysResourceRepository;
import cn.lau.auth.repository.SysResourceRoleRepository;
import cn.lau.auth.repository.SysRoleRepository;
import cn.lau.auth.repository.SysUserRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = ApiApplication.class)
@WebAppConfiguration
public class InitAccount {

    @Autowired
    private SysResourceRepository sysResourceRepository;
    @Autowired
    private SysResourceRoleRepository sysResourceRoleRepository;
    @Autowired
    private SysRoleRepository sysRoleRepository;
    @Autowired
    private SysUserRepository sysUserRepository;
    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;


    @Test
    public void initAccountTest(){

        SysRole role=new SysRole();

        role.setName("ADMIN");
        Set<SysRole> sysRoles=new HashSet<SysRole>();
        sysRoles.add(role);
        SysRole save1 = sysRoleRepository.save(role);
        System.out.println(save1.toString());
        //插入用户
        SysUser sysUser=new SysUser("admin","管理员",bCryptPasswordEncoder.encode("123456"),new Date(),new Date(),sysRoles);
        role.setSysUser(sysUser);
        SysUser save = sysUserRepository.save(sysUser);
        System.out.println(save.toString());

    }

}
